package  {
	
	import fl.controls.CheckBox;
	import fl.controls.ComboBox;
	import fl.events.ComponentEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import fl.controls.TextInput;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author Eppic Creative
	 */
	public class Hud extends Sprite {
		
		private var main:Main;
		
		private var txtDepth:TextInput;
		private var txtWidth:TextInput;
		private var boxHeight:ComboBox;
		private var boxOverhang:ComboBox;
		private var boxRoofPitch:ComboBox;
		
		private var checkboxRidgevent:CheckBox;
		private var checkboxWainscoating:CheckBox;
		public var boxRoofCovering:ComboBox;
		
		private var updateBtn:Sprite;
		
		private var errorMsg:TextField;
		private var errorTween:Tween
		private var errorMsgTimer:Timer;
		
		public var sidingColor:ColorPicker;
		public var trimColor:ColorPicker;
		public var roofColor:ColorPicker;
		public var wainscotingColor:ColorPicker;
		
		
		public function Hud(inMain:Main) {
			main = inMain;
			initForm();
			initInputValues();
			updateBtn.addEventListener(MouseEvent.CLICK, updateRendering);
			initColorPicker(main.getBarnTextures());
		}
		
		private function initForm():void {
			txtDepth = main.inputDepth;
			txtWidth = main.inputWidth;
			boxHeight = main.inputHeight;
			boxOverhang = main.inputOverhang;
			boxRoofPitch = main.inputRoofPitch;
			
			checkboxRidgevent = main.inputRidgevent;
			checkboxWainscoating = main.inputWainscoating;
			boxRoofCovering = main.inputRoofCovering;
			
			updateBtn = main.updateButton;
			errorMsg = main.errorMessage;
			errorMsg.visible = false;
			checkboxRidgevent.selected = true;
			
			sidingColor = main.sidingColor;
			trimColor = main.trimColor;
			roofColor = main.roofColor;
			wainscotingColor = main.wainscotingColor;
			
			wainscotingColor.disable();
			checkboxWainscoating.addEventListener(ComponentEvent.BUTTON_DOWN, wainscotingColor.toggleEnabledCheckbox);
			boxRoofCovering.addEventListener(Event.CHANGE, roofColor.toggleEnabledDropdown);
		}
		
		private function initInputValues():void {
			txtDepth.restrict = "0-9";
			txtWidth.restrict = "0-9";
			txtDepth.maxChars = 3;
			txtWidth.maxChars = 3;
			updateFields();
			
			boxHeight.addItem( { label:"8ft", data:8 } );
			boxHeight.addItem( { label:"10ft", data:10 } );
			boxHeight.addItem( { label:"12ft", data:12 } );
			boxHeight.addItem( { label:"14ft", data:14 } );
			boxHeight.addItem( { label:"16ft", data:16 } );
			
			boxOverhang.addItem({label:"none", data:(5)});
			boxOverhang.addItem({label:"12in", data:(1*Main.PIXLES_PER_FOOT)});
			boxOverhang.addItem({label:"18in", data:(1.5*Main.PIXLES_PER_FOOT)});
			boxOverhang.addItem( { label:"24in", data:(2*Main.PIXLES_PER_FOOT)} );
			
			boxRoofPitch.addItem( { label:"12/12", data:45 } );
			boxRoofPitch.addItem( { label:"11/12", data:42.51 } );
			boxRoofPitch.addItem( { label:"10/12", data:39.81 } );
			boxRoofPitch.addItem( { label:"9/12", data:36.87 } );
			boxRoofPitch.addItem( { label:"8/12", data:33.69 } );
			boxRoofPitch.addItem( { label:"7/12", data:30.26 } );
			boxRoofPitch.addItem( { label:"6/12", data:26.57 } );
			boxRoofPitch.addItem( { label:"5/12", data:22.62 } );
			boxRoofPitch.addItem( { label:"4/12°", data:18.43 } );
			boxRoofPitch.addItem( { label:"3/12°", data:14.04 } );
			boxRoofPitch.addItem( { label:"2/12", data:9.46 } );
			
			boxRoofCovering.addItem( { label:"Painted Steel", data:false } );
			boxRoofCovering.addItem( { label:"Shingles", data:true } );
		}
		
		private function initColorPicker(textures:Array):void {
			sidingColor.init(textures);
			roofColor.init(textures);
			trimColor.init(textures);
			wainscotingColor.init(textures);
		}
		
		public function updateRendering(e:MouseEvent):void {
			try {
				main.createTempStructure();
				main.setStructureOptions(checkboxRidgevent.selected, Boolean(checkboxWainscoating.selected));
				main.resizeStructure(Number(txtWidth.textField.text), Number(txtDepth.textField.text), boxHeight.selectedItem.data, boxRoofPitch.selectedItem.data, boxOverhang.selectedItem.data);
			} catch (e:Error) {
				showErrorMessage(e.message);
			}
			clearFocus();
			updateFields();
		}
		
		private function updateFields():void {
			txtWidth.text = String(main.getWidth());
			txtDepth.text = String(main.getDepth());
		}
		
		private function showErrorMessage(message:String):void {
			errorMsg.text = message;
			errorMsg.alpha = 0;
			errorMsg.visible = true;
			errorTween = new Tween(errorMsg, "alpha", None.easeIn, 0, 1, .75, true);
			errorTween.start();
			errorMsgTimer = new Timer(4750, 1);
			errorMsgTimer.addEventListener(TimerEvent.TIMER_COMPLETE, clearErrorMessage);
			errorMsgTimer.start();
		}
		
		private function clearErrorMessage(e:TimerEvent):void {
			errorTween = new Tween(errorMsg, "alpha", Regular.easeIn, 1, 0, 1.5, true);
			errorTween.start();
		}
		
		public function clearFocus():void {
			boxRoofPitch.focusEnabled = false;
			boxRoofPitch.dropdown.focusEnabled = false;
			boxOverhang.focusEnabled = false;
			boxOverhang.dropdown.focusEnabled = false;
			boxHeight.focusEnabled = false;
			boxHeight.dropdown.focusEnabled = false;
			txtDepth.drawFocus(false);
			txtWidth.drawFocus(false);
			
			checkboxRidgevent.focusEnabled = false;
			checkboxWainscoating.focusEnabled = false;
			boxRoofCovering.focusEnabled = false;
			boxRoofCovering.dropdown.focusEnabled = false;
		}
		
	}

}